package com.gxuwz.medical.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.domain.role.Role;
/**
 * 角色管理控制模块
 * @author 沉羽墨玖
 *
 */
public class RoleServlet extends BaseServlet {
	@Override
	protected void doGet(HttpServletRequest request,HttpServletResponse response)
				throws ServletException,IOException{
				this.doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request,HttpServletResponse response)
				throws ServletException,IOException{
			String m = request.getParameter("m");
			if("list".equals(m)){
				process(request,response,"/page/role/role_list.jsp");
			}else if("input".equals(m)){
				process(request,response,"/page/role/role_add.jsp");
			}else if("get".equals(m)){
				process(request,response,"/page/role/role_edit.jsp");
			}else if("add".equals(m)){
				add(request,response);
			}else if("edit".equals(m)){
				edit(request,response);
			}else if("del".equals(m)){
				del(request,response);
			}else{
				error(request,response);
			}
		
	}

	private void list(HttpServletRequest request,HttpServletResponse response)
				throws ServletException,IOException{
		process(request,response,"/page/role/role_list.jsp");
		
	}
	 
		/**
		 * 修改角色信息
		 * @param request
		 * @param response
		 * @throws ServletException
		 * @throws IOException
		 */
	private void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		//1:接收页面参数
		String roleId=request.getParameter("roleId");
		String roleName=request.getParameter("roleName");
		//页面权限复选框勾选的值
		String fid=request.getParameter("fid");
		//2:实例化角色role
		try{
			String[] menuIds=null;
			if(fid!=null&&!"".equals(fid)){
				menuIds=fid.split(",");
			}
			Role role=new Role(roleId,roleName);
			role.editRole(menuIds);
			list(request,response);
		}catch(Exception e){
			e.printStackTrace();
		}

	}
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		//1:接收页面参数
				String roleId=request.getParameter("roleId");
				String roleName=request.getParameter("roleName");
				//页面权限复选框勾选的值
				String fid=request.getParameter("fid");
				//2:实例化角色role
				try{
					String[] menuIds=null;
					if(fid!=null&&!"".equals(fid)){
						menuIds=fid.split(",");
					}
					Role role=new Role(roleId,roleName);
					role.addRole(roleName, menuIds);
					list(request,response);
				}catch(Exception e){
					e.printStackTrace();
				}

			}
	
	private void del(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		String roleId=request.getParameter("roleId");
		//实例化角色Role
		Role role=new Role();
		//调用方法
		try{
			System.out.println(roleId);
			//删除记录
			role.delRole(roleId);
			process(request,response,"/page/role/role_list.jsp");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
    
}
